package cn.nfwx.tv.adapter;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.TextUtils;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.fragment.nyqf.LiveFragment;
import cn.nfwx.tv.fragment.nyqf.MeFragment;
import cn.nfwx.tv.fragment.nyqf.NewFragment;
import cn.nfwx.tv.fragment.nyqf.ProgramFragment;
import cn.nfwx.tv.model.NyqfBean;

public class CategoryAdapter extends FragmentStatePagerAdapter {

    List<NyqfBean.DataBean> mDataBeans;

    public CategoryAdapter(FragmentManager fm) {
        super(fm);
        mDataBeans = new ArrayList<>();
    }

    public void setNewData(List<NyqfBean.DataBean> dataBeans) {
        mDataBeans.clear();
        mDataBeans.addAll(dataBeans);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataBeans.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return mDataBeans.get(position).getTitle();
    }

    @NonNull
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public Fragment getItem(int i) {
        if (TextUtils.isEmpty(mDataBeans.get(i).getCode())) {
            if (TextUtils.isEmpty(mDataBeans.get(i).getTitle())) {
                return MeFragment.newInstance();
            } else {
                if (mDataBeans.get(i).getTitle().equals("直播")) {
                    return LiveFragment.newInstance();
                } else if (mDataBeans.get(i).getTitle().equals("我")) {
                    return MeFragment.newInstance();
                } else {
                    return ProgramFragment.newInstance();
                }
            }
        } else {
            return NewFragment.newInstance(mDataBeans.get(i).getTitle(), mDataBeans.get(i).getCode());
        }
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }
}
